import Vue from 'vue'
import i18n from '@/locale'
export const reminds = ['error', 'success', 'error', 'warning', 'error']
export function replaceStr(str, arr) {
  const regArr = str.match(/\-[0-9]\-/g)
  let temp = str
  if (regArr && arr.length) {
    for (let i = 0; i < regArr.length; i++) {
      temp = temp.replace(new RegExp(regArr[i], 'g'), arr[i])
    }
    return temp
  } else {
    return str
  }
}

export function tips(obj) {
  Vue.prototype.$notify({
    message: obj.message,
    type: reminds[obj.code],
    duration: 3500
  })
}

export function betterTips(res, ...arg) {
  if (res.code === 1) {
    arg.forEach(fn => {
      if (typeof fn === 'function') {
        fn()
      }
    })
    Vue.prototype.$notify({
      message: replaceStr(i18n.t(String(res.message)), res.data),
      type: reminds[res.code],
      duration: 3500
    })
  } else {
    Vue.prototype.$notify({
      message: replaceStr(i18n.t(String(res.message)), res.data),
      type: reminds[res.code],
      duration: 0
    })
  }
}
